from PySide6.QtCore import Qt
from PySide6.QtWidgets import QLabel, QVBoxLayout, QStackedWidget, QWidget
from qfluentwidgets import Pivot

from app.view.data_interface import DataInterface
from app.view.template_interface import TemplateInterface


class MainWindow(QWidget):

    def __init__(self):
        super().__init__()
        self.pivot = Pivot(self)
        self.stackedWidget = QStackedWidget(self)
        self.vBoxLayout = QVBoxLayout(self)

        self.templateInterface = TemplateInterface(self)
        self.dataInterface = DataInterface(self)
        # self.checkInterface = QLabel('预览及检查', self)

        # 添加标签页
        self.addSubInterface(self.templateInterface, 'templateInterface', '选择模板')
        self.addSubInterface(self.dataInterface, 'dataInterface', '选择数据')
        # self.addSubInterface(self.checkInterface, 'checkInterface', '预览及检查')

        # 连接信号并初始化当前标签页
        self.stackedWidget.currentChanged.connect(self.onCurrentIndexChanged)
        self.stackedWidget.setCurrentWidget(self.templateInterface)
        self.pivot.setCurrentItem(self.templateInterface.objectName())

        self.vBoxLayout.setContentsMargins(30, 0, 30, 30)
        self.vBoxLayout.addWidget(self.pivot, 0, Qt.AlignHCenter)
        self.vBoxLayout.addWidget(self.stackedWidget)
        self.resize(800, 800)

    def addSubInterface(self, widget: QLabel, objectName: str, text: str):
        widget.setObjectName(objectName)
        # widget.setAlignment(Qt.AlignCenter)
        self.stackedWidget.addWidget(widget)

        # 使用全局唯一的 objectName 作为路由键
        self.pivot.addItem(
            routeKey=objectName,
            text=text,
            onClick=lambda: self.stackedWidget.setCurrentWidget(widget)
        )

    # 切换标签页
    def onCurrentIndexChanged(self, index):
        widget = self.stackedWidget.widget(index)
        self.pivot.setCurrentItem(widget.objectName())
